<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
      class="formList"
    >
      <el-row>
        <!-- <el-col :span="6"> -->
          <el-form-item label="供应商" prop="supplierUid">
            <el-select
              v-model="queryParams.supplierUid"
              placeholder="请选择供应商"
              clearable
              size="small"
              style="width: 232px"
            >
              <el-option
                v-for="dict in supplierUidOptions"
                :key="dict.supplierUid"
                :label="dict.supplierName"
                :value="dict.supplierUid"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col>
        <el-col :span="6"> -->
          <el-form-item label="联系人" prop="contactPerson">
            <el-input
              v-model="queryParams.contactPerson"
              placeholder="请输入联系人"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 232px"
            />
          </el-form-item>
        <!-- </el-col>
        <el-col :span="6"> -->
          <el-form-item label="手机号" prop="supplierPhone">
            <el-input
              v-model="queryParams.supplierPhone"
              placeholder="请输入入驻手机号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 232px"
            />
          </el-form-item>
        <!-- </el-col> -->
      </el-row>
      <el-row>
        <!-- <el-col :span="6"> -->
          <el-form-item label="状态" prop="dataStatus">
            <el-select
              v-model="queryParams.dataStatus"
              placeholder="请选择状态"
              clearable
              size="small"
              style="width: 232px"
            >
              <el-option
                v-for="dict in dataStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col>
        <el-col :span="6"> -->
          <el-form-item label="发布状态" prop="isAuth">
            <el-select
              v-model="queryParams.isAuth"
              placeholder="请选择发布状态"
              clearable
              size="small"
              style="width: 232px"
            >
              <el-option
                v-for="dict in isAuthOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="createTimeArray">
            <el-date-picker
              v-model="queryParams.createTimeArray"
              type="daterange"
              align="right"
              value-format="yyyy-MM-dd"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 232px"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
        <!-- </el-col> -->
      </el-row>
      <el-row>
         <el-form-item style="margin-left: 22px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置
            </el-button
            >
          </el-form-item>
      </el-row>

    </el-form>

    <el-row class="row-mbinfo" style="margin-left: 0px; margin-right: 0px;">
      <el-row class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['supplier:info:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">

        </el-col>
        <el-col :span="1.5">

        </el-col>
        <el-col :span="1.5">

        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-row>

    <div class="divTable">
      <el-table
        border
        v-loading="loading"
        :data="infoList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>

        <el-table-column
          label="LOGO"
          align="center"
          :show-overflow-tooltip="true"
          prop="supplierLogUrl"
        >
          <template slot-scope="scope">
            <el-image v-if="scope.row.supplierLogUrl != null && scope.row.supplierLogUrl != '' " slot="reference"
                      :src="scope.row.supplierLogUrl"
                      class="image-rows" :preview-src-list="[scope.row.supplierLogUrl]"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称" min-width="110" align="center" prop="supplierName"/>
        <el-table-column label="联系人" align="center" prop="contactPerson"/>
        <el-table-column label="联系电话" width="120" align="center" prop="supplierPhone"/>
        <el-table-column label="状态" align="center" prop="dataStatus">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.dataStatus"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="发布状态"
          align="center"
          prop="isAuth"
          :formatter="isAuthFormat"
        />
        <el-table-column label="创建时间" width="180" align="center" prop="createTime"/>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="130"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="startStatusUpdate(scope.row, (statu = 1))"
              v-if="scope.row.isAuth === '0'"
              v-hasPermi="['supplier:info:edit']"
            >发布
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-zoom-in"
              @click="handleShowDetail(scope.row)"
              v-hasPermi="['supplier:info:query']"
            >查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['supplier:info:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              v-if="scope.row.dataStatus == '0'"
              @click="handleDelete(scope.row)"
              v-hasPermi="['supplier:info:remove']"
            >删除
            </el-button>
            <!-- <el-button -->
            <!-- size="mini" -->
            <!-- size="mini" -->
            <!-- type="text" -->
            <!-- icon="el-icon-close" -->
            <!-- @click="startStatusUpdate(scope.row, (statu = 0))" -->
            <!-- v-if="scope.row.isAuth === '1'" -->
            <!-- v-hasPermi="['supplier:info:edit']" -->
            <!-- >停用 -->
            <!-- </el-button> -->
          </template>
        </el-table-column>

      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

<!--<div style="height: 400px">-->

<!--  <wechatImg :imageStr.sync="imgform.feedsImg" :imageurl.sync="imgform.liveurl"></wechatImg>-->

<!--</div>-->
  </div>
</template>

<script>
  // import wechatImg from "@/components/ImageUpload/weChatBirectImg";
  import {

    listInfo,
    getInfo,
    delInfo,
    addInfo,
    updateInfo,
    updateDataStatus,
    getSupplierList,
    updateIsAuth
  } from '@/api/supplier/info'

  import {getRegion} from '@/api/common/regin'
  import imageArray from '@/components/ImageUpload/imageArray'
  import imageList from '@/components/ImageUpload/imageList'
  import imageUpload from '@/components/ImageUpload'

  export default {
    name: 'Info',
    components: {
      // wechatImg,
      imageList,
      imageUpload,
      imageArray
    },
    data() {
      return {
        imgform: {
          imageurl: null,
          shareImg: null,
          feedsImg: null,
          liveurl: null,
          coverImg: null,
          liveBGurl: null,
        },
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        //发布状态翻译
        showDataIsAuth: '',
        //查看详细省市区翻译
        regionName: '',
        //图片预览
        zIndex: 3000,
        //文本域自动调节
        autosize: true,
        //宣传图
        bannerImage: [],
        //营业执照
        licenseImgUrl: [],
        region: [],
        // 地区信息
        options: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 供应商资料表格数据
        infoList: [],
        // 弹出层标题
        title: '',
        //查看详细标题
        viewTitle: '',
        //选中数组的名字
        names: [],
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 状态字典
        dataStatusOptions: [],
        // 发布状态字典
        isAuthOptions: [],
        //供应商列表
        supplierUidOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          supplierName: null,
          supplierPhone: null,
          contactPerson: null,
          dataStatus: null,
          supplierUid: null,
          isAuth: null,
          createTime: null,
          settledState: 1
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          supplierName: [
            {required: true, message: '供应商名称不能为空', trigger: 'blur'}
          ],
          mainCategory: [
            {required: true, message: '主营品类不能为空', trigger: 'blur'}
          ],
          region: [{required: true, message: '地址不能为空', trigger: 'blur'}],
          licenseImgUrl: [
            {required: true, message: '营业执照不能为空', trigger: 'blur'}
          ],

          supplierLogUrl: [
            {required: true, message: '供应商logo图不能为空', trigger: 'blur'}
          ],
          servePhone: [
            {required: true, message: '服务电话不能为空', trigger: 'blur'}
          ],
          supplierPhone: [
            {required: true, message: '入驻手机号不能为空', trigger: 'blur'}
          ],
          supplierAddress: [
            {required: true, message: '详细地址不能为空', trigger: 'blur'}
          ],
          contactPerson: [
            {required: true, message: '联系人不能为空', trigger: 'blur'}
          ],
          contactPhone: [
            {required: true, message: '联系电话不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    created() {
      this.getList()
      this.getDicts('sys_data_status').then((response) => {
        this.dataStatusOptions = response.data
      })
      this.getDicts('supplier_is_auth').then((response) => {
        this.isAuthOptions = response.data
      })
      getSupplierList("all").then((response) => {
        this.supplierUidOptions = response.data
      })
    },
    watch: {
      $route(to, from) {
        //from 从哪来
        //to //到哪去
        let path = from.path;
        if ("/supplier/add/data/" == path) {
          //刷新页面
          this.getList();
        } else {
          let index = path.lastIndexOf("/");
          if (index > -1) {
            path = path.substr(0, index);
            if ("/supplier/add/data/" == path) {
              //刷新页面
              this.getList();
            }
          }
        }
      }
    },
    methods: {
      /** 查询供应商资料列表 */
      getList() {
        this.loading = true
        listInfo(this.queryParams).then((response) => {
          this.infoList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus)
      },
      // 发布状态字典翻译
      isAuthFormat(row, column) {
        return this.selectDictLabel(this.isAuthOptions, row.isAuth)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          supplierUid: null,
          platformUid: null,
          supplierNo: null,
          mainCategory: null,
          supplierBrand: null,
          supplierName: null,
          supplierLogUrl: null,
          servePhone: null,
          supplierRemark: null,
          supplierEmail: null,
          supplierPhone: null,
          supplierProvince: null,
          supplierCity: null,
          supplierArea: null,
          supplierAddress: null,
          contactPerson: null,
          contactPhone: null,
          dataStatus: null,
          isAuth: null,
          applyUser: null,
          applyTime: null,
          licenseImgUrl: null,
          authTime: null,
          authUpdateUid: null,
          createUser: null,
          updateUser: null,
          updateTime: null,
          createTime: null,
          storeLng: null,
          storeLat: null,
          bannerImage: null,
          juli: null,
          auditRemark: null,
          orderService: null,
          dataSort: null,
          region: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.supplierUid)
        this.names = selection.map((item) => item.supplierName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i]
            if ('1' === element.dataStatus) {
              this.multiple = true
              break
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$router.push("/supplier/add/data/");
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.$router.push("/supplier/detail/data/"+row.supplierUid);
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$router.push("/supplier/update/data/"+row.supplierUid);
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate((valid) => {
          let area = this.$refs['cascaderAddr'].getCheckedNodes()[0].path
          this.form.supplierProvince = area[0]
          this.form.supplierCity = area[1]
          this.form.supplierArea = area[2]
          if (valid) {
            if (this.form.supplierUid != null) {
              updateInfo(this.form).then((response) => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addInfo(this.form).then((response) => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const supplierUids = row.supplierUid || this.ids
        const names = row.supplierName || this.names
        this.$confirm(
          '是否确认删除供应商资料编号为"' + names + '"的数据项?',
          '警告',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(function () {
            return delInfo(supplierUids)
          })
          .then(() => {
            this.getList()
            this.msgSuccess('删除成功')
          })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('supplier/info/export', {...this.queryParams}, `supplier_info.xlsx`)
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === '0' ? '无效' : '有效'
        this.$confirm(
          '确定要把"' + row.supplierName + '"供应商,设为' + text + '吗？',
          '警告',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(function () {
            return updateDataStatus(row.supplierUid, row.dataStatus)
          })
          .then(() => {
            this.msgSuccess('操作成功')
            this.getList()
          })
          .catch(function () {
            row.dataStatus = row.dataStatus === '0' ? '1' : '0'
          })
      },
      startStatusUpdate(row, statu) {
        let text
        if (statu === 0) {
          text = '停用'
        }
        if (statu === 1) {
          text = '发布'
        }
        let supplierName = row.supplierName
        this.$confirm(
          '是否"' + text + '"名称为"' + supplierName + '的供应商?',
          '警告',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(function () {
            let startStatus = statu
            return updateIsAuth(row.supplierUid, startStatus)
          })
          .then(() => {
            this.getList()
            this.msgSuccess(text + '成功')
          })
      }
    }
  }
</script>

<style scoped>
  .MmsUiBlockTitle___line___27lhp1-44-1 {
    display: inline-block;
    background: #407cff;
    width: 2px;
    height: 16px;
    margin-right: 8px;
  }

  .MmsUiBlockTitle___title___2C-H-1-44-1 {
    display: inline-block;
    font-size: 20px;
    color: #333;
    position: relative;
  }
</style>
